package com.spyx.service.impl;

import com.atyx.spyx.model.search.SkuEs;
import com.spyx.mapper.SkuMapper;
import com.spyx.service.SkuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Service
public class SkuServiceImpl  implements SkuService {
    @Autowired
    private SkuMapper skuMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public void incrHotScore(Long skuId) {
        // 定义key
        String hotKey = "hotScore";
        // 保存数据
        Double hotScore = redisTemplate.opsForZSet().incrementScore(hotKey, "skuId:" + skuId, 1);
        if (hotScore%10==0) {
            //1.先从es中获取skuEs对象
            Optional<SkuEs> optional = skuMapper.findById(skuId);
            SkuEs skuEs = optional.get();
            skuEs.setHotScore(skuEs.getHotScore() + 1);
            //2.保存数据
            skuMapper.save(skuEs);
        }
    }
}
